package com.btl.component.mp.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.btl.boot.core.util.SpringContextHolder;
import io.github.linpeilie.Converter;

import java.util.List;
import java.util.Map;

/**
 * 转换工具
 */
public class ConvertUtils {

    private final static Converter CONVERTER = SpringContextHolder.getBean(Converter.class);

    private ConvertUtils() {

    }

    /**
     * 分页转换
     *
     * @param page    源对象
     * @param voClass 返回类型
     * @return Page<P>
     */
    public static <T, P> Page<P> toPageVo(IPage<T> page, Class<P> voClass) {
        Page<P> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        if (page.getRecords()
                .isEmpty()) {
            return voPage;
        }
        voPage.setRecords(CONVERTER.convert(page.getRecords(), voClass));
        return voPage;
    }


    public static <S, T> T convert(S source, Class<T> targetType) {
        return CONVERTER.convert(source, targetType);
    }

    public static <S, T> T convert(S source, T target) {
        return CONVERTER.convert(source, target);
    }

    public static <S, T> List<T> convert(List<S> source, Class<T> targetType) {
        return CONVERTER.convert(source, targetType);
    }

    public static <T> T convert(Map<String, Object> map, Class<T> target) {
        return CONVERTER.convert(map, target);
    }
}
